package com.example.itcompanyinterview;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;

import com.example.itcompanyinterview.container.MapListImageAndTextListAdapter;
import com.example.itcompanyinterview.object.Role;
import com.example.itcompanyinterview.tools.CommonJsonParser;
import com.example.itcompanyinterview.tools.CommonUrlRequest;
import com.example.itcompanyinterview.tools.CustomTitleSetter;
import com.example.itcompanyinterview.tools.LoadingDialogHelper;
import com.example.itcompanyinterview.tools.MenuHelper;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class RoleListActivity extends Activity {

	private Handler messageHandler;
	private static String ROLE_CONTROLER = "Role";
	private static String GET_ROLE_LIST_ACTION = "GetRoles";
	private static int GET_ROLE_LIST = 0x02;
	
	private LoadingDialogHelper loadingHelper;
	private CustomTitleSetter _titleBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_role_list);
		this.initActivityTitleBar();
		this.loadingHelper = new LoadingDialogHelper(this);
		this.initMessageHandler();
		this.setRoleListClickListener();
		this.getRoleListFromUrl();
	}
	
    private void initActivityTitleBar() {
    	_titleBar = new CustomTitleSetter(this);
        _titleBar.setTitle("岗位列表", View.GONE, View.VISIBLE);
    }

	private void initMessageHandler() {
		this.messageHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (msg.what == GET_ROLE_LIST) {
					loadingHelper.closeLoadingDialog();
					String roleJsonString = (String) msg.obj;
					if (roleJsonString == null) {
						Toast.makeText(RoleListActivity.this, "获取岗位列表失败，请检测网络",
								Toast.LENGTH_SHORT).show();
						return;
					}
					initRolesList(roleJsonString);
				}
				super.handleMessage(msg);
			}

		};
	}

	public void setRoleListClickListener() {
		ListView detailListView = (ListView) this.findViewById(R.id.rolelist);
		detailListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View arg1, int pos,
					long id) {
				Role role = (Role)parent.getItemAtPosition(pos);
				Bundle bundle = new Bundle();
				bundle.putSerializable("role", role);
				Intent intent = new Intent(RoleListActivity.this,
						QuestionListActivity.class);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
	}

	private void getRoleListFromUrl() {
		new Thread(new Runnable() {
			public void run() {
				String roleJsonString = CommonUrlRequest
						.getControllerAndAction(
								RoleListActivity.ROLE_CONTROLER,
								RoleListActivity.GET_ROLE_LIST_ACTION);
				Message m = messageHandler.obtainMessage();
				m.obj = roleJsonString;
				m.what = GET_ROLE_LIST;
				messageHandler.sendMessage(m);
			}
		}).start();
		this.loadingHelper.showLoadingDialog();
	}

	public void initRolesList(String roleJsonString) {
		try {
			ListView roleListView = (ListView) this.findViewById(R.id.rolelist);
			JSONObject roleJsonObj = CommonJsonParser
					.parseJsonObject(roleJsonString);
			if (roleJsonObj.getInt("err") != 0) {
				Toast.makeText(RoleListActivity.this,
						"发生了异常:" + roleJsonObj.getString("msg"),
						Toast.LENGTH_LONG).show();
				return;
			}

			List<Role> roleList = new ArrayList<Role>();

			JSONArray roleJsonList = CommonJsonParser
					.parseJsonArray(roleJsonObj.getString("value"));
			int roleNum = roleJsonList.length();
			for (int i = 0; i < roleNum; i++) {
				JSONObject roleObj = roleJsonList.getJSONObject(i);
				int roleId = roleObj.getInt("id");
				String roleName = roleObj.getString("title");
				String roleImage = roleObj.getString("icon");
				Role role = new Role(roleId, roleName, roleImage);
				roleList.add(role);
			}
			
			MapListImageAndTextListAdapter adapter = 
	    			new MapListImageAndTextListAdapter(this, roleList, roleListView);
			roleListView.setAdapter(adapter);
		} catch (Exception e) {
			Log.e("InitRoleList", e.getMessage());
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	MenuHelper menuHelper = new MenuHelper(this, item);
    	menuHelper.processMenu();
    	return super.onOptionsItemSelected(item);
    }
}
